package com.hxkj.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxkj.dao.DictItemDao;
import com.hxkj.dao.DictTypeDao;
import com.hxkj.domain.enums.DictItemExceptionEnum;
import com.hxkj.domain.po.DictItem;
import com.hxkj.domain.po.DictType;
import com.hxkj.service.DictTypeService;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.to.PageTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 一级字典 实现类
 * <p>
 * Created by zzx on 2018/5/19.
 */
@Service
public class DictTypeServiceImpl implements DictTypeService {
    @Autowired
    private DictTypeDao dictTypeDao;

    @Autowired
    private DictItemDao dictItemDao;


    @Override
    public PageInfo<DictType> getDictTypeList(DictType dictType, PageTo pageParam) throws Exception {
        Page<DictType> page = PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        dictTypeDao.get(dictType);
        PageInfo<DictType> pageInfo = new PageInfo<>(page);
        return pageInfo;
    }

    @Override
    public ResultDataStruct getDictTypeFromCache(DictType dictType) throws Exception {
        ResultDataStruct resul = new ResultDataStruct();
        DictType dict;
        try {
            //通过 id 查询一级字典信息
            dict = dictTypeDao.get(dictType.getDictTypeId());
        } catch (Exception e) {
            e.printStackTrace();
            resul.setCode(DictItemExceptionEnum.DICTITEM_GET_ERROR.getCode());
            resul.setMessage(DictItemExceptionEnum.DICTITEM_GET_ERROR.getMsg());
            return resul;
        }
        resul.setData(dict);

        return resul;
    }

    @Override
    public ResultMessageStruct updateDictType(DictType dictType) {
        ResultMessageStruct resul = new ResultMessageStruct();
        try {
            //判断一级字典是否已经存在，如果存在则不允许重复添加 (包含自己可以修改自己)
            List<DictType> dictTypeName = dictTypeDao.selectDictName(dictType);
            if (dictTypeName.size() > 0) {
                resul.setCode(DictItemExceptionEnum.DICTITEM_NAME_ERROR.getCode());
                resul.setMessage(DictItemExceptionEnum.DICTITEM_NAME_ERROR.getMsg());
                return resul;
            }
            dictTypeDao.update(dictType);
        } catch (Exception e) {
            e.printStackTrace();
            resul.setCode(DictItemExceptionEnum.DICTITEM_MODIFY_ERROR.getCode());
            resul.setMessage(DictItemExceptionEnum.DICTITEM_MODIFY_ERROR.getMsg());
            return resul;
        }
        return resul;
    }

    @Override
    public ResultMessageStruct deleteDictType(DictType dictType) {
        ResultMessageStruct resul = new ResultMessageStruct();
        DictItem dictItem = new DictItem();
        dictItem.setDictTypeCode(dictType.getDictTypeCode());
        try {
            //判断一级字典下是否存在 二级字典， 如果存在不能删除
            List<DictItem> dictTypeName = dictItemDao.get(dictItem);
            if (dictTypeName.size() > 0) {
                resul.setCode(DictItemExceptionEnum.DICTITEM_DEL_ERROR.getCode());
                resul.setMessage(DictItemExceptionEnum.DICTITEM_DEL_ERROR.getMsg());
                return resul;
            }
            dictTypeDao.delete(dictType.getDictTypeId());
        } catch (Exception e) {
            e.printStackTrace();
            resul.setCode(DictItemExceptionEnum.DICTITEM_DELETE_ERROR.getCode());
            resul.setMessage(DictItemExceptionEnum.DICTITEM_DELETE_ERROR.getMsg());
            return resul;
        }
        return resul;
    }

    @Override
    public ResultMessageStruct saveDictType(DictType dictType) {
        ResultMessageStruct resul = new ResultMessageStruct();
        try {
            // 1.判断一级字典是否已经存在，如果存在则不允许重复添加 (包含自己可以修改自己)
            // 2.并且判断了 一级字典的 Code 也不能重复
            List<DictType> dictTypeName = dictTypeDao.selectDictName(dictType);
            if (dictTypeName.size() > 0) {
                resul.setCode(DictItemExceptionEnum.DICTITEM_NAME_ERROR.getCode());
                resul.setMessage(DictItemExceptionEnum.DICTITEM_NAME_ERROR.getMsg());
                return resul;
            }
            dictTypeDao.save(dictType);
        } catch (Exception e) {
            e.printStackTrace();
            resul.setCode(DictItemExceptionEnum.DICTITEM_MODIFY_ERROR.getCode());
            resul.setMessage(DictItemExceptionEnum.DICTITEM_MODIFY_ERROR.getMsg());
            return resul;
        }
        return resul;
    }

    @Override
    public ResultMessageStruct modifyDictType(DictType dictType) {
        ResultMessageStruct resul = new ResultMessageStruct();
        DictItem dictItem = new DictItem();
        dictItem.setDictTypeCode(dictType.getDictTypeCode());
        try {
            // 判断一级字典下是否存在 二级字典， 如果存在提示不能禁用
            List<DictItem> dictTypeName = dictItemDao.get(dictItem);
            if (dictTypeName.size() > 0) {
                resul.setCode(DictItemExceptionEnum.DICTITEM_DEL_TAG_ERROR.getCode());
                resul.setMessage(DictItemExceptionEnum.DICTITEM_DEL_TAG_ERROR.getMsg());
                return resul;
            }
            dictTypeDao.update(dictType);
        } catch (Exception e) {
            e.printStackTrace();
            resul.setCode(DictItemExceptionEnum.DICTITEM_DELETE_ERROR.getCode());
            resul.setMessage(DictItemExceptionEnum.DICTITEM_DELETE_ERROR.getMsg());
            return resul;
        }
        return resul;
    }

    @Override
    public ResultDataStruct<DictType> getDictType(DictType dictType) {
        ResultDataStruct<DictType> result = new ResultDataStruct<>();
        DictType dict;
        try {
            //通过 id 查询一级字典信息
            dict = dictTypeDao.getDictType(dictType);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(DictItemExceptionEnum.DICTITEM_GET_ERROR.getCode());
            result.setMessage(DictItemExceptionEnum.DICTITEM_GET_ERROR.getMsg());
            return result;
        }
        result.setData(dict);

        return result;
    }
}
